<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.mapper.${modelName}Mapper">

	<resultMap id="objMap" type="${packageName}.entity.${modelName}">
    <#list columns as col>
	    <result property="${col.propertyName}" column="${col.columnName}"/>
    </#list>
	</resultMap>

	<sql id="allField">
    <#list columns as col>
	    ${col.columnName}<#if col_has_next>,</#if>
    </#list>
	</sql>

	<sql id="conditions">
    <#list columns as col>
		<if test="${col.propertyName} != null">and ${col.columnName} = ${r"#{"}${col.propertyName}}</if>
	    <#if col.javaType == "String">
			<if test="${col.propertyName}Like != null">and ${col.columnName} like
				concat("%", ${r"#{"}${col.propertyName}Like}, "%")
			</if>
	    </#if>
	    <#if col.javaType == "Date">
		    <if test="${col.propertyName}Gte != null">and ${col.columnName} >= ${r"#{"}${col.propertyName}Gte}</if>
		    <if test="${col.propertyName}Lte != null"><![CDATA[ and ${col.columnName} <= ${r"#{"}${col.propertyName}Lte}]]>
		    </if>
	    </#if>
    </#list>
	</sql>

	<select id="select" parameterType="hashmap" resultMap="objMap">
		select
		<include refid="allField"/>
		from ${tableName}
		<where>
			<include refid="conditions"/>
		</where>
	</select>

	<select id="count" parameterType="hashmap" resultType="Long">
		select
		count(*)
		from ${tableName}
		<where>
			<include refid="conditions"/>
		</where>
	</select>

	<select id="getDataList" parameterType="com.xzops.bnm.common.model.datatables.DataTablesReqt" resultMap="objMap">
		select
		<include refid="allField" />
		from ${tableName}
		<where>
			<include refid="Common.dtSearchCondition"/>
		</where>
		<include refid="Common.dtparams_order"/>
	</select>

	<insert id="insert" parameterType="${packageName}.entity.${modelName}">
		insert into ${tableName}(
		<include refid="allField"/>
		)values(
    <#list columns as col>
	    ${r"#{"}${col.propertyName}}<#if col_has_next>,</#if>
    </#list>
		)
	</insert>

	<update id="update" parameterType="${packageName}.entity.${modelName}">
		update ${tableName}
		<set>
        <#list columns as col>
	        <if test="${col.propertyName} != null">${col.columnName} = ${r"#{"}${col.propertyName}}<#if col_has_next>,</#if></if>
        </#list>
		</set>
		where id = ${r'#{id}'}
	</update>

	<delete id="delete" parameterType="hashmap">
		delete from ${tableName}
		<where>
			<include refid="conditions"/>
		</where>
	</delete>

	<insert id="insertBatch" parameterType="list">
		insert into ${tableName}(
		<include refid="allField"/>
		)values
		<foreach collection="list" index="index" item="item" separator=",">
			<trim prefix="(" suffix=")" suffixOverrides=",">
			<#list columns as col>
				${r'#{item.'}${col.propertyName}${r'}'}<#if col_has_next>,</#if>
			</#list>
			</trim>
		</foreach>
	</insert>

	<delete id="batchDelete" parameterType="java.util.List">
		delete from ${tableName} where id in
		<foreach item="id" collection="list" open="(" separator="," close=")">
			${r'#{id}'}
		</foreach>
	</delete>

</mapper>